<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div>
        <button data-path="/home">首页</button>
        <button data-path="/music">音乐</button>
        <button data-path="/movie">电影</button>
    </div>
    <div class="content"></div>

    <script>
        const btns = document.querySelectorAll("button")
        const oContent = document.querySelector(".content")


        const routers = [{
                path: "/home",
                component: "这是首页"
            },
            {
                path: "/music",
                component: "音乐列表"
            },
            {
                path: "/movie",
                component: "热门电影"
            },
        ]

        btns.forEach(item => {
            item.onclick = function () {
                history.pushState(null, null, this.dataset.path)


                routers.forEach(item => {
                    if (item.path === this.dataset.path) {
                        oContent.innerHTML = item.component
                    }
                })
            }
        })

        window.onpopstate = function () {
            routers.forEach(item => {
                if (item.path === location.pathname) {
                    oContent.innerHTML = item.component
                }
            })
        }
    </script>
</body>

</html>